package gui.NetworkManager;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import control.LoginController;
import control.ReadOnlyDataController;

import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import vcpClasses.Client;

import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.toedter.calendar.JDateChooser;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JScrollBar;
/**
 * this class use to read data for reports Screen 
 * @author G8
 *
 */
public class ReadOnlyDataScreen extends JPanel implements ActionListener{
	public JButton btnLogout;
	public JButton btnBack;
	private JButton btnView;
	private JComboBox comboBoxType;
	private ReadOnlyDataController readOnlyDataController;
	private JTextArea textAreaReportContent;
	/**
	 * Read Only Data Screen Constructor 
	 */
	public ReadOnlyDataScreen(){
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize() {
		readOnlyDataController = new ReadOnlyDataController(null);
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Read Only Data", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(ReadOnlyDataScreen.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		add(btnBack);
		
		btnLogout = new JButton("Logout");
		btnLogout.setSize(150, 30);
		btnLogout.setBounds(393, 22, 78, 35);
		btnLogout.addActionListener(this);
		add(btnLogout);
		
		btnView = new JButton("View");
		btnView.setBounds(181, 99, 78, 20);
		btnView.addActionListener(this);
		add(btnView);
		
		JLabel lblDataType = new JLabel("Data Type:");
		lblDataType.setBounds(69, 71, 102, 14);
		add(lblDataType);
		
		comboBoxType = new JComboBox();
		comboBoxType.setModel(new DefaultComboBoxModel(new String[] {"Orders", "Workers", "Reports"}));
		comboBoxType.setBounds(181, 68, 138, 20);
		comboBoxType.addItem("Subscribed clients");
		comboBoxType.addItem("Clients");
		comboBoxType.addItem("Vehicles");
		comboBoxType.addItem("Parking lots");
		add(comboBoxType);
		
		JLabel lblReportContent = new JLabel("Content:");
		lblReportContent.setBounds(68, 131, 92, 14);
		add(lblReportContent);
		
		textAreaReportContent = new JTextArea();
		textAreaReportContent.setEditable(false);
		
		JScrollPane scroll = new JScrollPane (textAreaReportContent, 
		JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scroll.setBounds(69, 156, 365, 189);
		add(scroll);
	}
	
	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnView){
			SystemTypes datatypeToGet = new SystemTypes((String) comboBoxType.getSelectedItem(),null);
			SystemTypes dataResult = readOnlyDataController.getReadOnlyData(datatypeToGet);
			if (dataResult.getSysMode().equals(SystemMode.OK)){
				textAreaReportContent.setText(dataResult.getObj().toString());
			}
			else
				JOptionPane.showMessageDialog(null, dataResult.getObj().toString(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		if (e.getSource() == btnBack){
			VCP.setScreen(this, VCP.prevScreen);
			VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
			VCP.prevScreen.setLayout(null);
			VCP.frame.setBounds(VCP.prevScreen.getBounds());
		}
		if (e.getSource() == btnLogout){
			SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
			boolean logoutRes = LoginController.LogOut(userToLogout);
			if (logoutRes)
				VCP.setGUI(0, this);
			else{
				JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
